package com.itheima.health.controller;

import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.service.MemberService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("member")
public class MemberController {

    @Reference
    private MemberService memberService;


    @RequestMapping("getMemberCount.do")
    public Result getMemberCount() {
        try {
            Map map = memberService.getMemberCount();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("getMemberPercentageBySex.do")
    public Result getMemberPercentageBySex() {
        try {
            List<Map> mapList = memberService.getMemberPercentageBySex();
            for (Map map : mapList) {
                String name = (String) map.get("name");
                if (name.equals("1")) {
                    map.put("name", "男");
                } else {
                    map.put("name", "女");
                }
            }
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("getMemberAgeGroup.do")
    public Result getMemberAgeGroup() {
        try {
            List<Map> memberAgeGroup = new ArrayList<>();
            List<Map> mapList = memberService.getMemberAgeGroup();
            for (Map map : mapList) {
                Set set = map.keySet();
                for (Object o : set) {
                    String str = (String) o;
                    HashMap hashMap = new HashMap();
                    hashMap.put("name",str);
                    hashMap.put("value",map.get(str));
                    memberAgeGroup.add(hashMap);
                }
            }
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, memberAgeGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
